<?php
/**
 * 导航栏组件
 * @author ycoe
 */
class NavWidget extends YWidget{
	/**
	 * 导航栏代码
	 * @var string
	 */
	public $code = null;
	public $thumb = '200x200';
	public $count = 5;
	public $title = null;
	
	private $nav = null;
	private $items = null;
	public function run(){
		if(empty($this->code)){
			return;
		}
		//加载组件资源
		$this->load_resource();
		
		$mod = new NavigationMondel();
		$condition = array(
			'site_id' 	=> SITE_ID,
			'code'		=> $this->code,
		);
		$this->nav = $mod->get_by_attrs($condition);
		if(!empty($this->nav) && isset($this->nav['items'])){
			$now = time();
			foreach ($this->nav['items'] as $index => $item){
				//过滤掉已经过期的
				if(isset($item['start'])){
					if($now < $item['start']){
						unset($this->nav['items'][$index]);
						continue;
					}
				}
				if(isset($item['end'])){
					if($now > $item['end']){
						unset($this->nav['items'][$index]);
						continue;
					}
				}
			}
			if(!empty($this->nav['items'])){
				$tree = new YTree($this->nav['items'], array(
					'id_column' 	=> 'id',
					'name_column'	=> 'title'
				));
				$this->items = $tree->get_children();
			}
		}
		unset($nav);
		
		if(empty($this->nav)){
			return;
		}
		$this->render($this->class_name . '/' . $this->tpl, array(
			'id'		=> $this->id,
			'nav' 		=> $this->nav,
			'items'		=> $this->items,
			'thumb' 	=> $this->thumb,
			'title'		=> $this->title,
		));
	}
}

?>